/**
 * 回形数
 * 1 2 3
 * 8 9 4
 * 7 6 5
 */

import java.util.Scanner;

public class LoopNum {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入回形数的长度：");
        int length = scan.nextInt();
        int[][] arr = new int[length][length];
        int step = length - 1;  //当前方向行走步数
        int row = 0; // 当前行
        int col = 0; //当前列
        int currentNum = 1; //当前数值
        while (step > 0) {
            //右移
            for (int i = 0; i < step; i++) {
                arr[row][col++] = currentNum++;
            }
            //下移
            for (int i = 0; i < step; i++) {
                arr[row++][col] = currentNum++;
            }
            //左移
            for (int i = 0; i < step; i++) {
                arr[row][col--] = currentNum++;
            }
            //上移
            for (int i = 0; i < step; i++) {
                arr[row--][col] = currentNum++;
            }
            step -= 2; //下一层，步数-2
            // 纠正方向，进入里面一层
            col++;
            row++;
        }
        //奇数列的最中心位置会忽略，需要补充
        if (length % 2 == 1) {
            arr[row / 2 + 1][col / 2 + 1] = currentNum;
        }

        for (int[] ints : arr) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}
